<?php
/**
 * User: zhangligang
 * Email: tzhang2450@163.com
 * Date: 2018/12/20
 * Time: 17:08
 */

namespace App\Http\Controllers\V1;

use App\Http\Controllers\Controller;
use App\Providers\MyJwt;
use App\Services\DepartmentService;
use App\Services\Service;
use App\Services\UserService;
use Illuminate\Http\Request;

class DepartmentController extends Controller
{

    private $jwtCode;

    public function __construct(Request $request)
    {
        parent::__construct($request);
        $authorization = $request->header('Authorization');
        $jwt = new MyJwt();
        $jwtCode = $jwt->decode($authorization, MyJwt::ENCODE_KEY);
        if (!$jwtCode || !array_key_exists('uid', $jwtCode)) {
            $this->sendJson([], 4031, '请授权');
        }
        $this->jwtCode = $jwtCode;
    }

    /**
     * 建议
     */
    public function advice()
    {
        $this->sendGet(function() {
            $page = $this->request->get('page', 1);
            $pageSize = $this->request->get('page_size', 10);
            $offset = ($page - 1) * $pageSize;
            $departmentId = $this->getDepartmentId();
            $data = Service::getByDepartmentId($departmentId, $pageSize, $offset);
            $this->sendDataTable($data);
        });
    }

    /**
     * 评分文件列表
     */
    public function contest()
    {
        $this->sendGet(function() {
            $limit = $this->request->get('limit', 10);
            $offset = $this->request->get('offset', 0);
            $data = Service::getContest($limit, $offset, 2);
            $this->sendDataTable($data);
        });
    }

    /**
     * 评分文件详情
     */
    public function contestDetail()
    {
        $this->sendGet(function() {
            $id = $this->request->get('id');
            $data = Service::getContestDetail($id);
            $this->sendJson($data);
        });
    }

    public function saveContest()
    {
        $this->sendPost(function() {
            $params = $this->request->get('params');
            DepartmentService::saveContest($params);
            $this->sendJson([]);
        });
    }

    public function deleteContest()
    {
        $this->sendPost(function() {
            $id = $this->request->get('id');
            DepartmentService::deleteContest($id);
            $this->sendJson([]);
        });
    }

    /**
     * 积分文件
     */
    public function scoreFile()
    {
        $this->sendGet(function() {
            $departmentId = $this->getDepartmentId();
            $year = $this->request->get('year');
            $data = DepartmentService::getScoreFile($departmentId, $year);
            $this->sendDataTable($data);
        });
    }

    public function scoreFileDetail()
    {
        $this->sendGet(function(){
            $id = $this->request->get('id');
            $departmentId = $this->getDepartmentId();
            $data = DepartmentService::getScoreFileDetail($id, $departmentId);
            $this->sendJson($data);
        });
    }

    public function saveScoreFileScore()
    {
        $this->sendPost(function(){
            $params = $this->request->get('params');
            $departmentId = $this->getDepartmentId();
            DepartmentService::saveScoreFileScore($params['id'], $departmentId, $params['score']);
            $this->sendJson([]);
        });
    }

    public function userScoreFiles()
    {
        $this->sendGet(function(){
            $departmentId = $this->getDepartmentId();
            $uid = $this->request->get('uid');
            $data = DepartmentService::getUserScoreFile($departmentId, $uid);
            $this->sendDataTable($data);
        });
    }

    public function userScoreFileDetail()
    {
        $this->sendGet(function(){
            $id = $this->request->get('id');
            $departmentId = $this->getDepartmentId();
            $uid = $this->request->get('user_id');
            $data = DepartmentService::getUserScoreFileDetail($id, $departmentId, $uid);
            $this->sendJson($data);
        });
    }

    public function saveUserScoreFileScore()
    {
        $this->sendPost(function(){
            $params = $this->request->get('params');
            $departmentId = $this->getDepartmentId();
            DepartmentService::saveUserScoreFileScore($params['id'], $departmentId, $params['uid'], $params['score']);
            $this->sendJson([]);
        });
    }

    /**
     * 加分列表
     */
    public function increaseScore()
    {
        $this->sendGet(function() {
            $search = $this->request->get('search', '');
            $limit = $this->request->get('limit', 10);
            $offset = $this->request->get('offset', 0);
            $uid = $this->getDepartmentId();
            $data = DepartmentService::getIncreaseScore($uid, $limit, $offset, $search);
            $this->sendDataTable($data);
        });
    }

    /**
     * 加分申请
     */
    public function saveIncreaseScore()
    {
        $this->sendPost(function() {
            $departmentId = $this->getDepartmentId();
            $params = $this->request->get('params');
            $params['role'] = 2;
            DepartmentService::saveIncreaseScore($departmentId, $params);
            $this->sendJson([]);
        });
    }

    /**
     * 打分
     */
    public function saveUserIncrease()
    {
        $this->sendPost(function() {
            $params = $this->request->get('params');
            $departmentId = $this->getDepartmentId();
            DepartmentService::saveUserIncrease($params['id'], $departmentId, $params['score']);
            $this->sendJson([]);
        });
    }

    /**
     * 加分详情
     */
    public function increaseScoreDetail()
    {
        $this->sendGet(function() {
            $id = $this->request->get('id');
            $data = Service::getIncreaseScoreDetail($id);
            $this->sendJson($data);
        });
    }

    public function userIncreaseScore()
    {
        $this->sendGet(function() {
            $limit = $this->request->get('limit', 10);
            $offset = $this->request->get('offset', 0);
            $uid = $this->request->get('uid');
            $data = UserService::getIncreaseScore($uid, $limit, $offset);
            $this->sendDataTable($data);
        });
    }

    /**
     * 删除加分申请
     */
    public function deleteIncreaseScore()
    {
        $this->sendPost(function() {
            $id = $this->request->get('id');
            DepartmentService::deleteIncreaseScore($id);
            $this->sendJson([]);
        });
    }

    /**
     * 星级
     */
    public function star()
    {
        $this->sendGet(function() {
            $departmentId = $this->getDepartmentId();
            $rule = DepartmentService::getLastRule();
            $info = DepartmentService::getDetail($departmentId);
            $advice = DepartmentService::getLastAdvice($departmentId);
            $data = [
                'rule' => $rule ? $rule['content'] : '',
                'score' => $info['score'],
                'advice' => $advice ? $advice['content'] : ''
            ];
            $this->sendJson($data);
        });
    }

    /**
     * 党员列表
     */
    public function users()
    {
        $this->sendGet(function() {
            $search = $this->request->get('search', '');
            $id = $this->getDepartmentId();
            $role = $this->request->get('role', 0);
            $page = $this->request->get('page', 1);
            $pageSize = $this->request->get('page_size', 10);
            $offset = ($page - 1) * $pageSize;
            $data = DepartmentService::getUsersByDepartmentId($id, $pageSize, $offset, $role, $search);
            $this->sendJson($data);
        });
    }

    public function simpleUsers()
    {
        $this->sendGet(function() {
            $id = $this->getDepartmentId();
            $data = DepartmentService::getSimpleUsers($id);
            $this->sendJson($data);
        });
    }

    public function userDetail()
    {
        $this->sendGet(function() {
            $userId = $this->request->get('user_id');
            $data = DepartmentService::getUserDetail($userId);
            $this->sendJson($data);
        });
    }

    public function deleteUser()
    {
        $this->sendPost(function() {
            $userId = $this->request->get('id');
            DepartmentService::deleteUser($userId);
            $this->sendJson([]);
        });
    }

    /**
     * 打分
     */
    public function setScore()
    {
        $this->sendPost(function() {
            $uid = $this->request->get('id');

            $this->sendJson([]);
        });
    }

    public function file()
    {
        $this->sendGet(function() {
            $id = $this->getDepartmentId();
            $data = DepartmentService::getDetail($id);
            $this->sendJson($data);
        });
    }

    public function saveFile()
    {
        $this->sendPost(function() {
            $params = $this->request->get('params');
            $params['id'] = $this->getDepartmentId();
            DepartmentService::save($params);
            $this->sendJson([]);
        });
    }

    public function adviceToUser()
    {
        $this->sendGet(function() {
            $uid = $this->request->get('user_id');
            $year = $this->request->get('year', 0);
            $page = $this->request->get('page', 1);
            $pageSize = $this->request->get('page_size', 10);
            $offset = ($page - 1) * $pageSize;
            $departmentId = $this->getDepartmentId();
            $data = DepartmentService::adviceToUser($uid, $pageSize, $offset, $departmentId, $year);
            $this->sendDataTable($data);
        });
    }

    public function saveAdvice()
    {
        $this->sendPost(function(){
            $params = $this->request->get('params');
            $departmentId = $this->getDepartmentId();
            DepartmentService::saveAdvice($params['user_id'], $departmentId, $params['content']);
            $this->sendJson([]);
        });
    }

    public function userLevel()
    {
        $this->sendGet(function() {
            $uid = $this->request->get('user_id');
            $departmentId = $this->getDepartmentId();
            $data = DepartmentService::getUserLevel($uid, $departmentId);
            $this->sendJson($data);
        });
    }

    public function saveUserLevel()
    {
        $this->sendPost(function(){
            $params = $this->request->get('params');
            $departmentId = $this->getDepartmentId();
            DepartmentService::saveUserLevel($params, $departmentId);
            $this->sendJson([]);
        });
    }

    private function getDepartmentId()
    {
        return DepartmentService::getDepartmentId($this->jwtCode['uid']);
    }
}